﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SLA
{
    /// <summary>
    /// Interaction logic for Gesture.xaml
    /// </summary>
    public partial class GestureWindow : Window
    {
        private ComboBox cmbGestos;
        private bool isAdding;

        public GestureWindow(ComboBox cmbGestos)
        {
            InitializeComponent();
            this.cmbGestos = cmbGestos;
            SetForAdding();
        }

        public GestureWindow(ComboBox cmbGestos, string name, string speech)
        {
            InitializeComponent();
            this.cmbGestos = cmbGestos;
            SetForEditing(name, speech);
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrWhiteSpace(txtGestureName.Text) &&
                !String.IsNullOrWhiteSpace(txtGestureSpeech.Text))
            {
                if (isAdding)
                {
                    object gesture = null;
                    foreach (var item in cmbGestos.Items)
                    {
                        var boxItem = (item as ComboBoxItem);
                        if (boxItem.Name.ToString() == txtGestureName.Text)
                        {
                            gesture = boxItem;
                            break;
                        }
                    }
                    if (gesture == null)
                    {
                        var name = txtGestureName.Text.Replace(" ", "");
                        var item = new ComboBoxItem()
                        {
                            Name = name,
                            Tag = txtGestureSpeech.Text,
                            Content = name
                        };
                        cmbGestos.Items.Add(item);
                        MessageBox.Show("Gesto agregado");
                        SetForAdding();
                        if (chkCrearOtro.IsChecked.HasValue && !chkCrearOtro.IsChecked.Value)
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Ese gesto ya ha sido agregado!");
                    }
                }
            }
            else
            {
                var text = String.Empty;
                if (String.IsNullOrWhiteSpace(txtGestureName.Text))
                {
                    text += "Por favor especifique un nombre!\n";
                }
                if (String.IsNullOrWhiteSpace(txtGestureSpeech.Text))
                {
                    text += "Por favor especifique el texto que sera hablado!\n";
                }
                if (!String.IsNullOrWhiteSpace(text))
                {
                    MessageBox.Show(text);
                }
            }
        }

        private void SetForAdding()
        {
            txtGestureName.IsEnabled = true;
            txtGestureName.Text = String.Empty;
            txtGestureSpeech.Text = String.Empty;
            this.Title = "Agregar gesto";
            isAdding = true;
        }

        private void SetForEditing(string name, string speech)
        {
            txtGestureName.IsEnabled = false;
            txtGestureName.Text = name;
            txtGestureSpeech.Text = speech;
            this.Title = "Editar gesto";
            isAdding = false;
        }
    }
}
